Unsubscribe Mautic contacts from automated unsubscribe emails

工作流概述

这是一个包含16个节点的复杂工作流,主要用于自动化处理各种任务。

工作流源代码

下载
{
  "meta": {
    "instanceId": "f0efd559def66ddc761033b0b2eb86ed3edec31121f2c1aa92ed05e63303529a"
  },
  "name": "Unsubscribe Mautic contacts from automated unsubscribe emails",
  "tags": [],
  "nodes": [
    {
      "id": "55d6a64b-88e2-4162-a93a-b31ad32b94fd",
      "name": "Gmail Trigger",
      "type": "n8n-nodes-base.gmailTrigger",
      "position": [
        140,
        860
      ],
      "parameters": {
        "filters": {
          "includeSpamTrash": true
        },
        "pollTimes": {
          "item": [
            {
              "mode": "everyMinute"
            }
          ]
        }
      },
      "credentials": {
        "gmailOAuth2": {
          "id": "3",
          "name": "Gmail account"
        }
      },
      "typeVersion": 1
    },
    {
      "id": "a697b58c-e0c8-42e0-8211-49caf46ce222",
      "name": "Is automated unsubscribe?",
      "type": "n8n-nodes-base.if",
      "position": [
        460,
        1000
      ],
      "parameters": {
        "conditions": {
          "string": [
            {
              "value1": "= {{ $json[\"To\"] }}",
              "value2": "unsubscribe",
              "operation": "contains"
            },
            {
              "value1": "={{ $json[\"From\"] }}",
              "value2": "={{ $node[\"Edit Fields\"].json[\"emailAddress\"] }}",
              "operation": "notEqual"
            }
          ]
        }
      },
      "typeVersion": 1,
      "alwaysOutputData": false
    },
    {
      "id": "72c76f4b-50da-481a-9c3e-204158f3a016",
      "name": "Add to unsubscribed segment",
      "type": "n8n-nodes-base.mautic",
      "position": [
        1520,
        720
      ],
      "parameters": {
        "resource": "contactSegment",
        "contactId": "={{ $json[\"id\"] }}",
        "segmentId": 3,
        "authentication": "oAuth2"
      },
      "credentials": {
        "mauticOAuth2Api": {
          "id": "4",
          "name": "Mautic account"
        }
      },
      "typeVersion": 1
    },
    {
      "id": "44c85f57-0716-476f-bea5-00efeddf908f",
      "name": "Remove newsletter segment",
      "type": "n8n-nodes-base.mautic",
      "position": [
        1520,
        920
      ],
      "parameters": {
        "resource": "contactSegment",
        "contactId": "={{ $json[\"id\"] }}",
        "operation": "remove",
        "segmentId": 1,
        "authentication": "oAuth2"
      },
      "credentials": {
        "mauticOAuth2Api": {
          "id": "4",
          "name": "Mautic account"
        }
      },
      "typeVersion": 1
    },
    {
      "id": "b26ddbb9-3209-458b-8e94-2854ed8bf8de",
      "name": "Reply Unsubscribe Message",
      "type": "n8n-nodes-base.gmail",
      "position": [
        1520,
        1140
      ],
      "parameters": {
        "message": "={{$node[\"Edit Fields\"].json[\"unsubscribeMessage\"]}}",
        "options": {},
        "messageId": "={{ $node[\"Gmail Trigger\"].json[\"id\"] }}",
        "operation": "reply"
      },
      "credentials": {
        "gmailOAuth2": {
          "id": "3",
          "name": "Gmail account"
        }
      },
      "typeVersion": 2
    },
    {
      "id": "34fc931b-f692-4383-a75b-76502c11452b",
      "name": "Add to Do Not Contact List",
      "type": "n8n-nodes-base.mautic",
      "disabled": true,
      "position": [
        1520,
        520
      ],
      "parameters": {
        "contactId": "{{ $json[\"id\"] }}",
        "operation": "editDoNotContactList",
        "authentication": "oAuth2",
        "additionalFields": {}
      },
      "credentials": {
        "mauticOAuth2Api": {
          "id": "4",
          "name": "Mautic account"
        }
      },
      "typeVersion": 1
    },
    {
      "id": "b5dd2d22-c367-4f30-a1b3-e3a767aec96b",
      "name": "Extract Email from 'From' Field",
      "type": "n8n-nodes-base.code",
      "position": [
        640,
        840
      ],
      "parameters": {
        "mode": "runOnceForEachItem",
        "jsCode": "var fromField = $input.item.json.From;
var extractedEmail;
if (fromField.includes('<') && fromField.includes('>')) {
    // From field is wrapped in carets
    var regex = /[^< ]+(?=>)/g;
    extractedEmail = fromField.match(regex)[0];
} else {
    // From field is not wrapped in carets
    extractedEmail = fromField;
}
return {json: {extractedEmail}}"
      },
      "typeVersion": 1
    },
    {
      "id": "f11e57b5-7834-4654-8793-42b1aa297730",
      "name": "Extract Unique Email Addresses",
      "type": "n8n-nodes-base.code",
      "position": [
        820,
        1000
      ],
      "parameters": {
        "jsCode": "// Access the input data using all() method
const inputData = $input.all();
const uniqueEmailsSet = new Set();

// Loop through each item, extract the email, and add it to the Set
inputData.forEach(item => {
    uniqueEmailsSet.add(item.json.extractedEmail);
});

// Convert the Set to an array of objects in the n8n format
const uniqueEmailsArray = Array.from(uniqueEmailsSet).map(email => {
    return { json: { extractedEmail: email } };
});

return uniqueEmailsArray;
"
      },
      "typeVersion": 2
    },
    {
      "id": "5e168e07-1a6b-4140-81b9-9d9ffb852f61",
      "name": "Get Mautic Contact ID from Email Address",
      "type": "n8n-nodes-base.mautic",
      "position": [
        1020,
        840
      ],
      "parameters": {
        "limit": 1,
        "options": {
          "search": "=email:{{ $json[\"extractedEmail\"] }}",
          "rawData": false
        },
        "operation": "getAll",
        "authentication": "oAuth2"
      },
      "credentials": {
        "mauticOAuth2Api": {
          "id": "4",
          "name": "Mautic account"
        }
      },
      "typeVersion": 1,
      "alwaysOutputData": false
    },
    {
      "id": "ad1a7b7a-230a-4098-b419-c93e3a6398a1",
      "name": "If Contact Exists in Mautic",
      "type": "n8n-nodes-base.if",
      "position": [
        1180,
        1060
      ],
      "parameters": {
        "conditions": {
          "string": [
            {
              "value1": "={{ $json[\"id\"] }}",
              "operation": "isNotEmpty"
            }
          ]
        }
      },
      "typeVersion": 1
    },
    {
      "id": "17b999f9-9c50-488a-b5d2-d98bbd566048",
      "name": "Sticky Note",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        200,
        500
      ],
      "parameters": {
        "content": "## Step 1
Set your email address and unsubscribe message in the edit fields node"
      },
      "typeVersion": 1
    },
    {
      "id": "11d28571-7335-4e53-a691-973412b6daef",
      "name": "Sticky Note1",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        140,
        1020
      ],
      "parameters": {
        "content": "## Step 2
Set your credentials in the Gmail trigger"
      },
      "typeVersion": 1
    },
    {
      "id": "2874fbc3-5735-471c-8c34-70854e0770bd",
      "name": "Sticky Note2",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        940,
        660
      ],
      "parameters": {
        "content": "## Step 3
Set credentials in the mautic nodes"
      },
      "typeVersion": 1
    },
    {
      "id": "1e51f13f-3fba-4b5b-8c88-eb792c4c0b40",
      "name": "Sticky Note3",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        1240,
        460
      ],
      "parameters": {
        "content": "## Step 4
Edit segments (add or remove) in the mautic nodes, optionally add to do not contact list."
      },
      "typeVersion": 1
    },
    {
      "id": "3b5f94df-864b-480c-a6f2-d572345e7d9a",
      "name": "Sticky Note4",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        580,
        440
      ],
      "parameters": {
        "width": 237.7703390037576,
        "height": 194.55974544175768,
        "content": "## Unsubscribe Mautic contacts from automated unsubscribe emails"
      },
      "typeVersion": 1
    },
    {
      "id": "69496c4b-254d-4a89-8ab1-9fe80cfaea14",
      "name": "Edit Fields",
      "type": "n8n-nodes-base.set",
      "position": [
        300,
        700
      ],
      "parameters": {
        "fields": {
          "values": [
            {
              "name": "emailAddress",
              "stringValue": "hello@example.com"
            },
            {
              "name": "unsubscribeMessage",
              "stringValue": "Your have successfully opted out from our marketing campaigns. Please reply if you believe this is an error."
            }
          ]
        },
        "options": {}
      },
      "typeVersion": 3.2
    }
  ],
  "active": false,
  "pinData": {},
  "settings": {
    "executionOrder": "v1"
  },
  "versionId": "",
  "connections": {
    "Edit Fields": {
      "main": [
        [
          {
            "node": "Is automated unsubscribe?",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Gmail Trigger": {
      "main": [
        [
          {
            "node": "Edit Fields",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Is automated unsubscribe?": {
      "main": [
        [
          {
            "node": "Extract Email from 'From' Field",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "If Contact Exists in Mautic": {
      "main": [
        [
          {
            "node": "Add to unsubscribed segment",
            "type": "main",
            "index": 0
          },
          {
            "node": "Remove newsletter segment",
            "type": "main",
            "index": 0
          },
          {
            "node": "Reply Unsubscribe Message",
            "type": "main",
            "index": 0
          },
          {
            "node": "Add to Do Not Contact List",
            "type": "main",
            "index": 0
          }
        ],
        [
          {
            "node": "Reply Unsubscribe Message",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Extract Unique Email Addresses": {
      "main": [
        [
          {
            "node": "Get Mautic Contact ID from Email Address",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Extract Email from 'From' Field": {
      "main": [
        [
          {
            "node": "Extract Unique Email Addresses",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Get Mautic Contact ID from Email Address": {
      "main": [
        [
          {
            "node": "If Contact Exists in Mautic",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  }
}

功能特点

  • 自动检测新邮件
  • AI智能内容分析
  • 自定义分类规则
  • 批量处理能力
  • 详细的处理日志

技术分析

节点类型及作用

  • Gmailtrigger
  • If
  • Mautic
  • Gmail
  • Code

复杂度评估

配置难度:
★★★★☆
维护难度:
★★☆☆☆
扩展性:
★★★★☆

实施指南

前置条件

  • 有效的Gmail账户
  • n8n平台访问权限
  • Google API凭证
  • AI分类服务订阅

配置步骤

  1. 在n8n中导入工作流JSON文件
  2. 配置Gmail节点的认证信息
  3. 设置AI分类器的API密钥
  4. 自定义分类规则和标签映射
  5. 测试工作流执行
  6. 配置定时触发器(可选)

关键参数

参数名称 默认值 说明
maxEmails 50 单次处理的最大邮件数量
confidenceThreshold 0.8 分类置信度阈值
autoLabel true 是否自动添加标签

最佳实践

优化建议

  • 定期更新AI分类模型以提高准确性
  • 根据邮件量调整处理批次大小
  • 设置合理的分类置信度阈值
  • 定期清理过期的分类规则

安全注意事项

  • 妥善保管API密钥和认证信息
  • 限制工作流的访问权限
  • 定期审查处理日志
  • 启用双因素认证保护Gmail账户

性能优化

  • 使用增量处理减少重复工作
  • 缓存频繁访问的数据
  • 并行处理多个邮件分类任务
  • 监控系统资源使用情况

故障排除

常见问题

邮件未被正确分类

检查AI分类器的置信度阈值设置,适当降低阈值或更新训练数据。

Gmail认证失败

确认Google API凭证有效且具有正确的权限范围,重新进行OAuth授权。

调试技巧

  • 启用详细日志记录查看每个步骤的执行情况
  • 使用测试邮件验证分类逻辑
  • 检查网络连接和API服务状态
  • 逐步执行工作流定位问题节点

错误处理

工作流包含以下错误处理机制:

  • 网络超时自动重试(最多3次)
  • API错误记录和告警
  • 处理失败邮件的隔离机制
  • 异常情况下的回滚操作